package com.demo.rydemo.d_user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.rydemo.d_user.entity.DUser;
import com.demo.rydemo.d_user.mapper.DUserMapper;
import com.demo.rydemo.d_user.service.IDUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.rydemo.d_user.vo.DUserVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ry
 * @since 2020-07-31
 */
@Service
public class DUserServiceImpl extends ServiceImpl<DUserMapper, DUser> implements IDUserService {

    /**
     * 获取用户列表
     * @param dUserVo
     * @return
     */
    @Override
    public IPage<DUser> getUserList(DUserVo dUserVo) {
        IPage<DUser> iPage = new Page<>();
        iPage.setCurrent(dUserVo.getPageIndex());
        iPage.setSize(dUserVo.getPageSize());
        QueryWrapper<DUser> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(dUserVo.getUserAccount())){
            queryWrapper.eq("user_account",dUserVo.getUserAccount());
        }
        if(StringUtils.isNotEmpty(dUserVo.getUserEmail())){
            queryWrapper.eq("user_email",dUserVo.getUserEmail());
        }

        queryWrapper.select("user_id","user_account","user_email","user_phone"
                ,"user_last_login_time","user_status");

        page(iPage,queryWrapper);

        return page(iPage,queryWrapper);
    }

    /**
     * 添加用户
     * @param dUserVo
     * @return
     */
    @Override
    public Boolean addUser(DUserVo dUserVo) {
        //设置 密码加密
        Md5Hash md5Hash =new Md5Hash(dUserVo.getUserPassword(),dUserVo.getUserAccount());
        dUserVo.setUserPassword(md5Hash.toString());
        Boolean flag = save(dUserVo);
        return flag;
    }

    /**
     * 修改用户信息
     * @param dUserVo
     * @return
     */
    @Override
    public Boolean editUser(DUserVo dUserVo) {
        return updateById(dUserVo);
    }
}
